package com.px.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.px.dto.ReturnResult;
import com.px.dto.ReturnResultUtils;
import com.px.pojo.ShCommodityTempStock;
import com.px.service.ShCommodityTempStockService;
import com.px.utils.RedisUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/shCommodityTempStock")
public class ShCommodityTempStockController {

    @Resource
    private ShCommodityTempStockService shCommodityTempStockService;

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private ObjectMapper objectMapper;

    @GetMapping("/list")
    public ReturnResult selectShCommodityTempStock(@RequestParam(required = false) String commodityName,
                                                   @RequestParam(required = false) Integer dicOneId,
                                                   @RequestParam(required = false) Integer dicTwoId,
                                                   @RequestParam(required = false) Double minPrice,
                                                   @RequestParam(required = false) Double maxPrice,
                                                   @RequestParam(required = false) String ram,
                                                   @RequestParam(required = false) String pageIndex,
                                                   @CookieValue(required = false) String account,
                                                   Model model)throws Exception{
        Integer currentPageNo =1;
        //获取页码
        if(!StringUtils.isEmpty(pageIndex)){
            currentPageNo = Integer.parseInt(pageIndex);
        }
        //控制页码
        if(currentPageNo<=0){
            currentPageNo = 1;
        }
        Integer pageSize = 5;
        //查询出总记录数
        Integer totalCount = shCommodityTempStockService.selectShCommodityTempStockCount(commodityName,dicOneId,dicTwoId,minPrice,maxPrice,ram);
        //根据总记录数算出总页数
        Integer temp = totalCount/pageSize;
        Integer totalPageCount = totalCount%pageSize ==0? temp:temp+1;
        if(currentPageNo>totalPageCount){
            currentPageNo=totalPageCount;
        }
        List<ShCommodityTempStock>  shCommodityTempStocks = shCommodityTempStockService.selectShCommodityTempStockStatus(commodityName, dicOneId,dicTwoId, minPrice,maxPrice,ram, currentPageNo, pageSize);
        model.addAttribute("shCommodityTempStocks",shCommodityTempStocks);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("currentPageNo",currentPageNo);
        model.addAttribute("totalPageCount",totalPageCount);
        model.addAttribute("name",commodityName);
        model.addAttribute("dicOneId",dicOneId);
        model.addAttribute("dicTwoId",dicTwoId);
        return ReturnResultUtils.returnSuccess(shCommodityTempStocks);
    }
}
